package sho.util 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.utils.*
	
	import mx.core.UIComponent;
	import mx.core.Application;
	import mx.containers.Box;
	import mx.managers.PopUpManager;
	import flash.display.DisplayObject;
	
	public class BitmapUtil 
	{
		static public function renderAsPopup(target: UIComponent) : UIComponent
		{
			// Use the render() function to create a bitmap object.
			var result : UIComponent = render(target);
			
			// Use the popup manager to float the bitmap above
			// the rest of the application.
			PopUpManager.addPopUp(result, DisplayObject(Application.application), false);
			
			// Get the location of the target in global coordinates.
			var p : Point = new Point(0, 0);
			p = target.localToGlobal(p);
			
			// Set the position of the popup to these coordinates.
			result.x = p.x;
			result.y = p.y;
			
			// Return the floating bitmap to the caller.
			return result;
		}
		
		static public function render(target: UIComponent) : UIComponent
		{
	        // Create the bitmap data object with the right size.
			var data : BitmapData = 
				new BitmapData(target.width, target.height, true, 0);
				
	        // Draw the target object into the bitmap data.
			data.draw(target);
			
	        // Create a new bitmap object associated with this data.
	        var bitmap: Bitmap = new Bitmap(data);
			
	        // Create a box object. This is one of the most 
	        // simple forms of UIComponent.
			var box : Box = new Box;
			
	        // Add the bitmap to its list of children. We must 
	        // use the "rawChildren" list, because the bitmap
	        // is a raw display list object, not a UIComponent.
			box.rawChildren.addChild(bitmap);
			
	        // Set the size of the box correctly.
			box.width = target.width;
			box.height = target.height;
			
			return box;
		}

	}
}